<?php
$rootPath = str_replace('wp-content/themes/wp-foundation/lib/extensions/options-backup', '' ,dirname(__FILE__));
require_once($rootPath . '/wp-config.php');

$fdoptions = $wpdb->get_results("SELECT * FROM $wpdb->options WHERE option_name like 'fd_%'") or die ("Cannot select options table.");
if ($fdoptions){

	//prep output
	$tab = "\t";
	$br = "\n";
	$xml = '<?xml version="1.0" encoding="UTF-8"?>'.$br;
	
	$xml.= '<records>'.$br;
	foreach($fdoptions as $option) {
		$xml.= $tab.'<record>'.$br;
		$xml.= $tab.$tab.'<option_id>' . $option->option_id . '</option_id>'.$br;
		$xml.= $tab.$tab.'<blog_id>' . $option->blog_id . '</blog_id>'.$br;
		$xml.= $tab.$tab.'<option_name>' . $option->option_name . '</option_name>'.$br;
		$xml.= $tab.$tab.'<option_value>' . htmlspecialchars(stripslashes($option->option_value)) . '</option_value>'.$br;
		$xml.= $tab.$tab.'<autoload>' . $option->autoload . '</autoload>'.$br;
		$xml.= $tab.'</record>'.$br;
	}
	
	$xml.= '</records>'.$br;
}

	//save file
	$filename = 'foundation-backup-'.time().'.xml';
	$handle = fopen($filename,'w+');
	fwrite($handle,$xml);
	fclose($handle);


$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
if (!$filename) {
	echo $err;
} else {
	$path = dirname(__FILE__) . '/' . $filename;
	if (file_exists($path) && is_readable($path)) {
		$size = filesize($path);
		header('Content-Type: application/octet-stream');
		header('Content-Length: '.$size);
		header('Content-Disposition: attachment; filename='.$filename);
		header('Content-Transfer-Encoding: binary');
		$file = @ fopen($path, 'rb');
			if ($file) {
				fpassthru($file);
				exit;
			} else {
				echo $err;
			}
	} else {
		echo $err;
	}
}
?>